package com.ruoyi.project.custom.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.custom.domain.CustomGroup;
import com.ruoyi.project.custom.service.ICustomGroupService;
import com.ruoyi.project.custom.service.ICustomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【客户分组】Controller
 * 
 * @author ruoyi
 * @date 2024-06-26
 */
@RestController
@RequestMapping("/custom/group")
public class CustomGroupController extends BaseController
{
    @Autowired
    private ICustomGroupService customGroupService;
    @Autowired
    private ICustomInfoService customInfoService;

    /**
     * 查询【客户分组】列表
     */
    @GetMapping("/list")
    public AjaxResult list(CustomGroup customGroup)
    {
        List<CustomGroup> list = customGroupService.selectCustomGroupList(customGroup);

        return AjaxResult.success(list);
    }

    /**
     * 导出【客户分组】列表
     */
    @Log(title = "【导出客户分组】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomGroup customGroup)
    {
        List<CustomGroup> list = customGroupService.selectCustomGroupList(customGroup);
        ExcelUtil<CustomGroup> util = new ExcelUtil<CustomGroup>(CustomGroup.class);
        return util.exportExcel(list, "group");
    }

    /**
     * 获取【客户分组】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customGroupService.selectCustomGroupById(id));
    }

    /**
     * 新增【客户分组】
     */
    @Log(title = "【新增客户分组】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomGroup customGroup)
    {
        if (UserConstants.NOT_UNIQUE.equals(customGroupService.checkGroupNameUnique(customGroup)))
        {
            return AjaxResult.error("新增客户分组'" + customGroup.getGroupName() + "'失败，客户分组名称已存在");
        }
        return toAjax(customGroupService.insertCustomGroup(customGroup));
    }

    /**
     * 修改【客户分组】
     */
    @Log(title = "【修改客户分组】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomGroup customGroup)
    {
        if (UserConstants.NOT_UNIQUE.equals(customGroupService.checkGroupNameUnique(customGroup)))
        {
            return AjaxResult.error("修改客户分组'" + customGroup.getGroupName() + "'失败，客户分组名称已存在");
        }
        return toAjax(customGroupService.updateCustomGroup(customGroup));
    }

    @Log(title = "【修改客户分组状态】", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody CustomGroup customGroup){
        if("0".equals(customGroup.getStatus())){
            // 检查是否存在可用的下级
            if(customGroupService.checkExistNormalChildren(customGroup) > 0){
                return AjaxResult.error("请先禁用所有下级分组！");
            }
            return toAjax(customGroupService.updateCustomGroup(customGroup));
        }else {
            // 检测是否存在禁用的上级
            if(customGroupService.checkExistUnNormalParent(customGroup) > 0){
                return AjaxResult.error("请先启用上级分组！");
            }
            return toAjax(customGroupService.updateCustomGroup(customGroup));
        }
    }

    /**
     * 删除【客户分组】
     */
    @Log(title = "【删除客户分组】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        if(customGroupService.hasChildByGroupId(id) > 0){
            return AjaxResult.error("存在子分组,不允许删除");
        }

        if(customInfoService.checkCustomExistGroup(id) > 0){
            return AjaxResult.error("该客户分组已被使用，不允许删除");
        }
        return toAjax(customGroupService.deleteCustomGroupById(id));
    }
}
